@beenotung/better-sqlite3-helper
A nodejs wrapper library for the work with better-sqlite3 ("The fastest and simplest library for SQLite3 in Node.js"). It's intended for simple server-apps for nodejs and offer some new functions and a migration-system.
This package is forked from better-sqlite3-helper by Kauto. Most changes were back-ported but some pull requests are still pending.
New in Version 4.x
- breaking: forked into
@beenotung/better-sqlite3-helper
for npm release - breaking: better-sqlite3 Version 9 is now used.
- feat:
@types/better-sqlite3
is added as optional and peer dependencies - feat: auto setup synchronous pragma when establish connection to database
- patch: fix type signature for creating new instance of DB
- chore: no longer depends on
mkdirp
, using the built-in version from fs
instead
New in Version 3.0
better-sqlite3 Version 7 is now used. This means that the option "memory" is removed (use path :memory:
instead - worked in version 2 too) and support for Node.js versions < 10 is dropped. For older node versions you can continue using version 2 of this library.
New in Version 2.0
All commands of better-sqlite3 Version 5 (like function and backup) can now be used too. Commands for Version 4 are removed. In addition there is now a TypeScript Declaration File for this library.
How to install
Install it for example with
npm i @beenotung/better-sqlite3-helper
How to use
In every file you want access to a sqlite3 database simply require the library and use it right away.
anyServerFile.js
const DB = require('@beenotung/better-sqlite3-helper')
let row = DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
console.log(row.firstName, row.lastName, row.email);
To setup your database, create a sql
-file named 001-init.sql
in a migrations
-directory in the root-directory of your program.
~/migrations/001-init.sql
CREATE TABLE `users` (
id INTEGER PRIMARY KEY,
firstName TEXT NOT NULL,
lastName TEXT NOT NULL,
email TEXT NOT NULL
);
DROP TABLE IF EXISTS `users`;
And that's it!
One global instance
A normal, simple application is mostly working with only one database. To make the class management more easy, this library does the access-control for you - mainly as a singleton. (But you can create a new instance to access other databases.)
The database loads lazy. Only when it's used for the first time, the database is read from the file, the migration is started and the journal-mode WAL is set. The default directory of the database is './data/sqlite3.db'
.
If you want to change the default-values, you can do this by calling the library once in the beginning of your server-code and thus setting it up:
index.js
const DB = require('@beenotung/better-sqlite3-helper')
DB({
path: './data/sqlite3.db',
readonly: false,
fileMustExist: false,
WAL: true,
migrate: {
force: false,
table: 'migration',
migrationsPath: './migrations'
}
})
After that you can use the library without parameter:
anotherAPIFile.js
const DB = require('@beenotung/better-sqlite3-helper')
let row = DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
console.log(row.firstName, row.lastName, row.email);
New Functions
This class implements shorthand methods for better-sqlite3.
let allUsers = DB().query('SELECT * FROM users');
let row = DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
let {id, firstname} = DB().queryFirstRowObject('SELECT * FROM users WHERE id=?', userId);
let email = DB().queryFirstCell('SELECT email FROM users WHERE id=?', userId);
let emails = DB().queryColumn('email', 'SELECT email FROM users');
let emailsByLastName = DB().queryKeyAndColumn('lastName', 'email', 'SELECT lastName, name FROM users');
Insert, Update and Replace
There are shorthands for update
, insert
, replace
and delete
. They are intended to make programming of CRUD-Rest-API-functions easier. With a blacklist
or a whitelist
it's even possible to send a request's query (or body) directly into the database.
Update
DB().update('users', {
lastName: 'Mustermann',
firstName: 'Max'
}, {
email: 'unknown@emailprovider.com'
})
DB().update('users', req.body, ['email = ?', req.body.email], ['lastName', 'firstName'])
DB().updateWithBlackList('users', req.body, req.body.id, ['id', 'email'])
Insert and replace
DB().insert('users', {
lastName: 'Mustermann',
firstName: 'Max',
email: 'unknown@emailprovider.com'
})
DB().insert('users', [{
lastName: 'Mustermann',
firstName: 'Max',
email: 'unknown@emailprovider.com'
}, {
lastName: 'Mustermann2',
firstName: 'Max2',
email: 'unknown2@emailprovider.com'
}])
DB().replace('users', req.body, ['lastName', 'firstName'])
DB().replaceWithBlackList('users', req.body, ['id', 'email'])
Delete
DB().delete('users', {id: 4})
Try and catch
If you want to put invalid values into the database, the functions will throw an error. So don't forget to surround the functions with a try-catch
. Here is an example for an express-server:
const { Router } = require('express')
const bodyParser = require('body-parser')
const DB = require('@beenotung/better-sqlite3-helper')
router.patch('/user/:id', bodyParser.json(), function (req, res, next) {
try {
if (!req.params.id) {
res.status(400).json({error: 'missing id'})
return
}
DB().updateWithBlackList(
'users',
req.body,
req.params.id,
['id']
)
res.statusCode(200)
} catch (e) {
console.error(e)
res.status(503).json({error: e.message})
}
})
Migrations
The migration in this library mimics the migration system of the excellent sqlite by Kriasoft.
To use this feature you have to create a migrations
-directory in your root. Inside you create sql
-files that are separated in a up- and a down-part:
migrations/001-initial-schema.sql
CREATE TABLE Category (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE Post (id INTEGER PRIMARY KEY, categoryId INTEGER, title TEXT,
CONSTRAINT Post_fk_categoryId FOREIGN KEY (categoryId)
REFERENCES Category (id) ON UPDATE CASCADE ON DELETE CASCADE);
INSERT INTO Category (id, name) VALUES (1, 'Business');
INSERT INTO Category (id, name) VALUES (2, 'Technology');
DROP TABLE Category
DROP TABLE Post;
migrations/002-missing-index.sql
CREATE INDEX Post_ix_categoryId ON Post (categoryId);
DROP INDEX Post_ix_categoryId;
The files need to be numbered. They are automatically executed before the first use of the database.
NOTE: For the development environment, while working on the database schema, you may want to set
force: 'last'
(default false
) that will force the migration API to rollback and re-apply the latest migration over again each time when Node.js app launches. See "Global Instance".
You can also give an array of changes.
const DB = require('@beenotung/better-sqlite3-helper')
const db = new DB({
migrate: {
migrations: [
`-- Up
CREATE TABLE Setting (
key TEXT NOT NULL UNIQUE,
value BLOB,
type INT NOT NULL DEFAULT 0,
PRIMARY KEY(key)
);
CREATE INDEX IF NOT EXISTS Setting_index_key ON Setting (key);
-- Down
DROP INDEX IF EXISTS Setting_index_key;
DROP TABLE IF EXISTS Setting;
`,
`-- Up
INSERT INTO Setting (key, value, type) VALUES ('test', 'now', 0);
INSERT INTO Setting (key, value, type) VALUES ('testtest', 'nownow', 6);
-- Down
DELETE FROM Setting WHERE key = 'test';
DELETE FROM Setting WHERE key = 'testtest';
`
]
}
})
More Documentation of better-sqlite3
License
MIT